package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.api.system.model.DictionaryDTO;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.system.convert.DictionaryConvert;
import com.xuecheng.system.entity.Dictionary;
import com.xuecheng.system.mapper.DictionaryMapper;
import com.xuecheng.system.service.DictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Override
    public DictionaryDTO getDicByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return null;
        }
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dictionary::getCode, code);
        Dictionary dictionary = this.getOne(queryWrapper);
        //需要判断dictionary的查询结果
        DictionaryDTO dictionaryDTO = null;
        if (dictionary != null) {
            dictionaryDTO = DictionaryConvert.INSTANCE.dictionaryPoToDto(dictionary);
        } else {
            dictionaryDTO = new DictionaryDTO();
        }
        return dictionaryDTO;
    }

    @Override
    public List<DictionaryDTO> queryAll() {

        List<Dictionary> dictionaries = this.list();

        return DictionaryConvert.INSTANCE.dictionaryPoToDtos(dictionaries);
    }
}
